package cn.youqu.happy.module.youqu.controller.admin.systemHobby;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.systemHobby.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.systemHobby.SystemHobbyDO;
import cn.youqu.happy.module.youqu.convert.systemHobby.SystemHobbyConvert;
import cn.youqu.happy.module.youqu.service.systemHobby.SystemHobbyService;

@Api(tags = "管理后台 - 系统爱好")
@RestController
@RequestMapping("/youqu/system-hobby")
@Validated
public class SystemHobbyController {

    @Resource
    private SystemHobbyService systemHobbyService;

    @PostMapping("/create")
    @ApiOperation("创建系统爱好")
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:create')")
    public CommonResult<Integer> createSystemHobby(@Valid @RequestBody SystemHobbyCreateReqVO createReqVO) {
        return success(systemHobbyService.createSystemHobby(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新系统爱好")
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:update')")
    public CommonResult<Boolean> updateSystemHobby(@Valid @RequestBody SystemHobbyUpdateReqVO updateReqVO) {
        systemHobbyService.updateSystemHobby(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除系统爱好")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:delete')")
    public CommonResult<Boolean> deleteSystemHobby(@RequestParam("id") Integer id) {
        systemHobbyService.deleteSystemHobby(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得系统爱好")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:query')")
    public CommonResult<SystemHobbyRespVO> getSystemHobby(@RequestParam("id") Integer id) {
        SystemHobbyDO systemHobby = systemHobbyService.getSystemHobby(id);
        return success(SystemHobbyConvert.INSTANCE.convert(systemHobby));
    }

    @GetMapping("/list")
    @ApiOperation("获得系统爱好列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:query')")
    public CommonResult<List<SystemHobbyRespVO>> getSystemHobbyList(@RequestParam("ids") Collection<Integer> ids) {
        List<SystemHobbyDO> list = systemHobbyService.getSystemHobbyList(ids);
        return success(SystemHobbyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得系统爱好分页")
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:query')")
    public CommonResult<PageResult<SystemHobbyRespVO>> getSystemHobbyPage(@Valid SystemHobbyPageReqVO pageVO) {
        PageResult<SystemHobbyDO> pageResult = systemHobbyService.getSystemHobbyPage(pageVO);
        return success(SystemHobbyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出系统爱好 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:system-hobby:export')")
    @OperateLog(type = EXPORT)
    public void exportSystemHobbyExcel(@Valid SystemHobbyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SystemHobbyDO> list = systemHobbyService.getSystemHobbyList(exportReqVO);
        // 导出 Excel
        List<SystemHobbyExcelVO> datas = SystemHobbyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "系统爱好.xls", "数据", SystemHobbyExcelVO.class, datas);
    }

}
